use std::io;
use rand::{thread_rng, Rng};
use std::cmp::Ordering;


fn main() {
    println!("guess the number");

    // gen_range 包含下限不包含上限
    let secret_number = thread_rng().gen_range(1, 101);

    // println!("the secret number is {}", secret_number);

    let mut i = 0;
    loop {
        i = i + 1;
        println!("please input your guess {}", i);
        let mut guess = String::new();

        io::stdin().read_line(&mut guess)
            .expect("failed to read line");

        let guess: u32 = match guess.trim().parse() {
            Ok(num) => num,
            Err(_) => {
                println!("please input number");
                continue;
            }
        };

        println!("you guessed: {}", guess);

        match guess.cmp(&secret_number) {
            Ordering::Less => println!("too small"),
            Ordering::Greater => println!("too big"),
            Ordering::Equal => {
                println!("you win!!");
                break;
            }
        };
        if i >= 5 {
            println!("the secret number is {}", secret_number);
            println!("you fail");
            break;
        }
    }
}